package com.dnb.webmash.playmaker.server;
import java.io.IOException; 
import java.util.Date;
import java.util.Properties; 
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Session; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.*; 
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;

@SuppressWarnings("serial")
public class MailHandlerServlet extends HttpServlet {
	private static final Logger LOG = Logger.getLogger(WebPlaylistServiceImpl.class.getName());
    public void doPost(HttpServletRequest req, 
                       HttpServletResponse resp) 
            throws IOException { 
        Properties props = new Properties(); 
        Session session = Session.getDefaultInstance(props, null); 
        try {
			MimeMessage message = new MimeMessage(session, req.getInputStream());
			
			//NOW SEND TO ADMIN GMAIL            
         // Create the message to forward
            Message forward = new MimeMessage(session);

            // Fill in header
            forward.setSubject("Fwd: " + message.getSubject());
            forward.setFrom(new InternetAddress("redirect@media-loop.appspotmail.com"));
            forward.addRecipient(Message.RecipientType.TO, new InternetAddress("watchtheweb.medialoop@gmail.com"));
            Date forwardedDate = new Date();
            String senders = new String();
            String recipients = new String();
            for (int i = 0; i < message.getFrom().length; i++)
            	senders += message.getFrom()[i];
            for (int i = 0; i < message.getAllRecipients().length ; i++)
            	recipients += message.getAllRecipients()[i];
            forward.setSubject("FW (received - "+forwardedDate +"; from - "+ senders +"; to - "+ recipients +") : " + message.getSubject());
            boolean excludedAtt = false;
            Multipart content = new MimeMultipart();
            content = (Multipart) message.getContent();
            String fwCont = new String();
            for (int i = 0; i < content.getCount(); i++){
            	BodyPart bp = content.getBodyPart(i);
            	if (bp.getContentType().contains("text")){
            		fwCont += bp.getContent().toString();
            		fwCont += "\n\n ********************** \n\n";
            	}            	
            	else excludedAtt = true;            	
            }
           
            if (excludedAtt) {
            	fwCont+="\n\n**********************************************************************************\n";
            	fwCont+="NOTICE: Part of this email or attachments were not included when forwarding.";
            }
			
            forward.setText(fwCont);

            // Send message
            Transport.send(forward);
		} catch (Exception e) {
			LOG.log(Level.WARNING, e.getLocalizedMessage());
			Message msg = new MimeMessage(session);
            try {
				msg.setFrom(new InternetAddress("mailerror@media-loop.appspotmail.com"));
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress("watchtheweb.medialoop@gmail.com"));
	            msg.setSubject("Error attempting to forward e-Mail");
	            msg.setText(e.toString());
	            Transport.send(msg);
			} catch (Exception e1) {
				LOG.log(Level.WARNING, e1.getLocalizedMessage());
			}            
		}
    }
}